home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / progtool / alle / rcom / lib / rsrc.c next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  15.4 KB  |  439 lines

  1. /*******************************************************************************
  2. *
  3. *  Project name : GENERAL C LIBRARY FOR TOS APPLICATIONS
  4. *  Module name  : Library for compiled resources
  5. *  Symbol prefix: rsrc
  6. *
  7. *  Author       : Harald Siegmund (HS)
  8. *  Co-Authors   : -
  9. *  Write access : HS
  10. *
  11. *  Notes        : -
  12. *-------------------------------------------------------------------------------
  13. *  Things to do : -
  14. *
  15. *-------------------------------------------------------------------------------
  16. *  History:
  17. *
  18. *  1990:
  19. *     Apr 05: introduced, implementation
  20. *     Jul 23: fast object coordinate fix function
  21. *     Oct 09: bug fixed: tree index overflow checking didn't work
  22. *             changing initialization (no longer use pointers which
  23. *              don't appear in the relocation table)
  24. *  1991:
  25. *     Jun 08: cosmetic changes
  26. *  1992:
  27. *     May 22: reorganize source
  28. *             adding rsrc_saddr() and rsrc_obfix()
  29. *     Jun 05/23: cosmetic changes
  30. *  1993:
  31. *     Apr 26: new file header
  32. *     Aug 30: replace RHDR _RSCHDR by LONGHDR _RSCLHDR (long header format)
  33. *
  34. *******************************************************************************/
  35. /*KEY _NAME="library for compiled resources" */
  36. /*END*/
  37.  
  38. /*******************************************************************************
  39. *                                                                              *
  40. *                                   INCLUDES                                   *
  41. *                                                                              *
  42. *******************************************************************************/
  43.  
  44. #include <stddef.h>
  45. #include <aes.h>
  46. /*KEY _END */
  47.  
  48.  
  49. /*******************************************************************************
  50. *                                                                              *
  51. *                                    MACROS                                    *
  52. *                                                                              *
  53. *******************************************************************************/
  54.  
  55. #define UNUSED(x) x = x             /* unused formal parameter */
  56.  
  57.  
  58. /*******************************************************************************
  59. *                                                                              *
  60. *                               TYPE DEFINITIONS                               *
  61. *                                                                              *
  62. *******************************************************************************/
  63.  
  64. typedef unsigned long ulong;        /* shortcut */
  65.  
  66. typedef struct                      /* resource file header (long format) */
  67.    {
  68.    ulong    vrsn;                   /* version # */
  69.    ulong    object;                 /* offset to first OBJECT */
  70.    ulong    tedinfo;                /* offset to first TEDINFO */
  71.    ulong    iconblk;                /* offset to first ICONBLK */
  72.    ulong    bitblk;                 /* offset to first BITBLK */
  73.    ulong    frstr;                  /* offset to ULONG array of offsets to */
  74.                                     /*  free strings */
  75.    ulong    string;                 /* offset to first string */
  76.    ulong    imdata;                 /* offset to base of image data */
  77.    ulong    frimg;                  /* offset to ULONG array of offsets to */
  78.                                     /*  free images */
  79.    ulong    trindex;                /* offset to ULONG array of offsets to */
  80.                                     /*  first OBJECT of trees */
  81.    ulong    nobs;                   /* # of OBJECTs */
  82.    ulong    ntree;                  /* # of trees */
  83.    ulong    nted;                   /* # of TEDINFOs */
  84.    ulong    nib;                    /* # of ICONBLKs */
  85.    ulong    nbb;                    /* # of BITBLKs */
  86.    ulong    nstring;                /* # of free strings */
  87.    ulong    nimages;                /* # of free images */
  88.    ulong    rssize;                 /* size of resource file */
  89.    } LONGHDR;
  90.  
  91.  
  92. /*******************************************************************************
  93. *                                                                              *
  94. *                               EXTERN REFERENCES                              *
  95. *                                                                              *
  96. *******************************************************************************/
  97.  
  98. extern LONGHDR _RSCLHDR;            /* address of resource header */
  99. extern OBJECT  _object[];           /* ^ to first OBJECT */
  100. extern TEDINFO _tedinfo[];          /* ^ to first TEDINFO */
  101. extern ICONBLK _iconblk[];          /* ^ to first ICONBLK */
  102. extern BITBLK  _bitblk[];           /* ^ to first BITBLK */
  103. extern OBJECT  *_trindex[];         /* ^ to tree ^ table */
  104. extern char    *_frstr[];           /* ^ to free string ^ table */
  105. extern BITBLK  *_frimg[];           /* ^ to free image ^ table */
  106.  
  107.  
  108. /*******************************************************************************
  109. *                                                                              *
  110. *                                LOCAL PROTOTYPES                              *
  111. *                                                                              *
  112. *******************************************************************************/
  113.  
  114.    /* fix one object coordinate/size */
  115. static int obfix(int v,int size);
  116.  
  117.  
  118. /*******************************************************************************
  119. *                                                                              *
  120. *                             UNINITIALIZED STATICS                            *
  121. *                                                                              *
  122. *******************************************************************************/
  123.  
  124. static int  chr_w,chr_h;            /* character width and height in pixels */
  125.  
  126.  
  127. /*******************************************************************************
  128. *                                                                              *
  129. *                                 LOCAL FUNCTIONS                              *
  130. *                                                                              *
  131. *******************************************************************************/
  132.  
  133. /*******************************************************************************
  134. *
  135. *  obfix: fix one object coordinate/size
  136. *
  137. *  An object coodinate or size is converted from screen-independend format to
  138. *  screen-dependend format.
  139. *
  140. *  screen-independend format:
  141. *     low byte  = character offset
  142. *     high byte = pixel offset
  143. *
  144. *  screen-dependend format:
  145. *     pixel offset
  146. *
  147. *  Out:
  148. *     value in screen-dependend format
  149. *
  150. *******************************************************************************/
  151.  
  152. static int obfix(
  153.  
  154. int         v,                      /* coordinate/size to fix */
  155. int         size)                   /* size factor (character width/height) */
  156. {
  157.  
  158.    return (v & 0xff) * size + ((v >> 8) & 0xff);
  159.  
  160. }     /* obfix() */
  161.  
  162.  
  163. /*******************************************************************************
  164. *                                                                              *
  165. *                                GLOBAL FUNCTIONS                              *
  166. *                                                                              *
  167. *******************************************************************************/
  168.  
  169. /*START*/
  170. /*******************************************************************************
  171. *
  172. *  rsrc_load: initialize resource
  173. *
  174. *  The object coordinate in the built-in resource are adjusted to the current
  175. *  system font size.
  176. *
  177. *  Out:
  178. *     status (always 1 = ok)
  179. *
  180. *******************************************************************************/
  181.  
  182. int rsrc_load(
  183.  
  184. const char  *re_lpfname)            /* ^ to filename of resource (IGNORED) */
  185. {
  186. /*END*/
  187.  
  188.    int      dummy;                  /* dummy buffer */
  189.    ulong    i;
  190.  
  191.  
  192.    UNUSED(re_lpfname);              /* we don't need the filename */
  193.  
  194.                                     /* get size of system font's characters */
  195.    (void)graf_handle(&chr_w,&chr_h,&dummy,&dummy);
  196.  
  197.                                     /* fix x/y/w/h of all objects */
  198.    for (i = 0; i < _RSCLHDR.nobs; rsrc_obfix(_object,(int)i++));
  199.  
  200.    return 1;
  201.  
  202. }     /* rsrc_load() */
  203.  
  204.  
  205. /*START*/
  206. /*******************************************************************************
  207. *
  208. *  rsrc_free: remove resource from memory
  209. *
  210. *  This version of rsrc_free() performs absolutely *nothing*!
  211. *
  212. *  Out:
  213. *     status (always 1 = ok)
  214. *
  215. *******************************************************************************/
  216.  
  217. int rsrc_free(void)
  218. {
  219. /*END*/
  220.  
  221.    return 1;
  222.  
  223. }     /* rsrc_free() */
  224.  
  225.  
  226. /*START*/
  227. /*******************************************************************************
  228. *
  229. *  rsrc_gaddr: get address of resource structure
  230. *
  231. *  Out:
  232. *     status:
  233. *     1                 ok
  234. *     0                 error (unknown type or invalid index)
  235. *
  236. *     ^ to structure in *gaddr (NULL if error occured)
  237. *
  238. *******************************************************************************/
  239.  
  240. int rsrc_gaddr(
  241.  
  242. int         re_gtype,               /* structure type (R_...) */
  243. int         re_gindex,              /* structure index */
  244. void        *gaddr)                 /* ^ to buffer for structure's address */
  245. {
  246. /*END*/
  247.  
  248.    int      stat;                   /* return status */
  249.  
  250.  
  251.    stat = (re_gindex >= 0);         /* index must be >= 0 !!! */
  252.  
  253.    if (stat) switch (re_gtype)      /* which structure type? */
  254.       {
  255.       case R_TREE:                  /* object tree */
  256.          if (re_gindex >= _RSCLHDR.ntree) stat = 0;
  257.          else *(OBJECT **)gaddr = _trindex[re_gindex];
  258.          break;
  259.  
  260.       case R_OBJECT:                /* object */
  261.          if (re_gindex >= _RSCLHDR.nobs) stat = 0;
  262.          else *(OBJECT **)gaddr = _object + re_gindex;
  263.          break;
  264.  
  265.       case R_TEDINFO:               /* text edit info */
  266.          if (re_gindex >= _RSCLHDR.nted) stat = 0;
  267.          else *(TEDINFO **)gaddr = _tedinfo + re_gindex;
  268.          break;
  269.  
  270.       case R_ICONBLK:               /* icon block */
  271.          if (re_gindex >= _RSCLHDR.nib) stat = 0;
  272.          else *(ICONBLK **)gaddr = _iconblk + re_gindex;
  273.          break;
  274.  
  275.       case R_BITBLK:                /* icon block */
  276.          if (re_gindex >= _RSCLHDR.nbb) stat = 0;
  277.          else *(BITBLK **)gaddr = _bitblk + re_gindex;
  278.          break;
  279.  
  280.       case R_STRING:                /* free string */
  281.          if (re_gindex >= _RSCLHDR.nstring) stat = 0;
  282.          else *(char **)gaddr = _frstr[re_gindex];
  283.          break;
  284.  
  285.       case R_IMAGEDATA:             /* free image */
  286.          if (re_gindex >= _RSCLHDR.nimages) stat = 0;
  287.          else *(BITBLK **)gaddr = _frimg[re_gindex];
  288.          break;
  289.  
  290.       case R_OBSPEC:                /* ob_spec */
  291.          if (re_gindex >= _RSCLHDR.nobs) stat = 0;
  292.          else *(long *)gaddr = _object[re_gindex].ob_spec.index;
  293.          break;
  294.  
  295.       case R_TEPTEXT:               /* text ^ */
  296.          if (re_gindex >= _RSCLHDR.nted) stat = 0;
  297.          else *(char **)gaddr = _tedinfo[re_gindex].te_ptext;
  298.          break;
  299.  
  300.       case R_TEPTMPLT:              /* template ^ */
  301.          if (re_gindex >= _RSCLHDR.nted) stat = 0;
  302.          else *(char **)gaddr = _tedinfo[re_gindex].te_ptmplt;
  303.          break;
  304.  
  305.       case R_TEPVALID:              /* validation ^ */
  306.          if (re_gindex >= _RSCLHDR.nted) stat = 0;
  307.          else *(char **)gaddr = _tedinfo[re_gindex].te_pvalid;
  308.          break;
  309.  
  310.       case R_IBPMASK:               /* icon mask ^ */
  311.          if (re_gindex >= _RSCLHDR.nib) stat = 0;
  312.          else *(int **)gaddr = _iconblk[re_gindex].ib_pmask;
  313.          break;
  314.  
  315.       case R_IBPDATA:               /* icon data ^ */
  316.          if (re_gindex >= _RSCLHDR.nib) stat = 0;
  317.          else *(int **)gaddr = _iconblk[re_gindex].ib_pdata;
  318.          break;
  319.  
  320.       case R_IPBTEXT:               /* icon text ^ */
  321.          if (re_gindex >= _RSCLHDR.nib) stat = 0;
  322.          else *(char **)gaddr = _iconblk[re_gindex].ib_ptext;
  323.          break;
  324.  
  325.       case R_BIPDATA:               /* image data ^ */
  326.          if (re_gindex >= _RSCLHDR.nbb) stat = 0;
  327.          else *(int **)gaddr = _bitblk[re_gindex].bi_pdata;
  328.          break;
  329.  
  330.       case R_FRSTR:                 /* free string ^ table */
  331.          if (re_gindex >= _RSCLHDR.nstring) stat = 0;
  332.          else *(char ***)gaddr = _frstr + re_gindex;
  333.          break;
  334.  
  335.       case R_FRIMG:                 /* free image ^ table */
  336.          if (re_gindex >= _RSCLHDR.nimages) stat = 0;
  337.          else *(BITBLK ***)gaddr = _frimg + re_gindex;
  338.          break;
  339.  
  340.       default:                      /* unknown */
  341.          stat = 0;
  342.       }
  343.  
  344.    if (!stat)                       /* error? */
  345.       *(char **)gaddr = NULL;       /* then clear return ^ */
  346.  
  347.    return stat;
  348.  
  349. }     /* rsrc_gaddr() */
  350.  
  351.  
  352. /*START*/
  353. /*******************************************************************************
  354. *
  355. *  rsrc_saddr: set address of resource structure
  356. *
  357. *  Out:
  358. *     status:
  359. *     1                 ok
  360. *     0                 error (unknown type or invalid index)
  361. *
  362. *******************************************************************************/
  363.  
  364. int rsrc_saddr(
  365.  
  366. int         re_stype,               /* structure type (R_FRSTR or R_FRIMG) */
  367. int         re_sindex,              /* structure index */
  368. void        *saddr)                 /* new address of structure */
  369. {
  370. /*END*/
  371.  
  372.    int      stat;                   /* return status */
  373.  
  374.  
  375.    stat = (re_sindex >= 0);         /* index must be >= 0 !!! */
  376.  
  377.    if (stat) switch (re_stype)      /* which structure type? */
  378.       {
  379.       case R_FRSTR:                 /* free string ^ table */
  380.          if (re_sindex >= _RSCLHDR.nstring) stat = 0;
  381.          else _frstr[re_sindex] = saddr;
  382.          break;
  383.  
  384.       case R_FRIMG:                 /* free image ^ table */
  385.          if (re_sindex >= _RSCLHDR.nimages) stat = 0;
  386.          else _frimg[re_sindex] = saddr;
  387.          break;
  388.  
  389.       default:                      /* unknown */
  390.          stat = 0;
  391.       }
  392.  
  393.    return stat;
  394.  
  395. }     /* rsrc_saddr() */
  396.  
  397.  
  398. /*START*/
  399. /*******************************************************************************
  400. *
  401. *  rsrc_obfix: adjust object coordinates to screen font size
  402. *
  403. *  Position and size of an object is converted from the screen-independend
  404. *  format (created by a resource editor) to the screen-dependend format
  405. *  (required to display dialogs).
  406. *
  407. *  Note: don't use this function before rsrc_load() has been called
  408. *
  409. *  Out:
  410. *     status:
  411. *     1                 ok
  412. *     0                 error (invalid object index)
  413. *
  414. *******************************************************************************/
  415.  
  416. int rsrc_obfix(
  417.  
  418. OBJECT      *re_otree,              /* ^ to object tree */
  419. int         re_oobject)             /* object index */
  420. {
  421. /*END*/
  422.  
  423.    if (re_oobject < 0)              /* invalid index? */
  424.       return 0;                     /* then abort */
  425.  
  426.    re_otree += re_oobject;          /* ^ to object */
  427.  
  428.    re_otree->ob_x      = obfix(re_otree->ob_x,chr_w);       /* fix x */
  429.    re_otree->ob_y      = obfix(re_otree->ob_y,chr_h);       /* fix y */
  430.    re_otree->ob_width  = obfix(re_otree->ob_width,chr_w);   /* fix width */
  431.    re_otree->ob_height = obfix(re_otree->ob_height,chr_h);  /* fix height */
  432.  
  433.    return 1;
  434.  
  435. }     /* rsrc_obfix() */
  436.  
  437.  
  438. /*EOF*/
  439.